package com.levelup.cr.web.controller;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.levelup.cr.client.rpc.RepositoryRemoteService;
import com.levelup.cr.model.Node;
import com.levelup.cr.model.dto.NodeTransfer;
import com.levelup.cr.services.NodeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 9/24/13
 * Time: 6:46 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class RepositoryRemoteController extends RemoteServiceServlet implements RepositoryRemoteService{
    private ServletContext servletContext;
    private final static String DEFAULT_PATH = File.separator+"level1";
    private final static Integer DEFAULT_DEPTH = 1;

    @Autowired
    private NodeListService nodeListService;

    public void setNodeListService(NodeListService nodeListService) {
        this.nodeListService = nodeListService;
    }

    @Override
    public ServletContext getServletContext() {
        return servletContext;
    }

    @Required
    @Autowired
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @RequestMapping(value = "/cr/repository.rpc", method = RequestMethod.POST)
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("Content-Type", "text/x-gwt-rpc;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            request.setCharacterEncoding("UTF-8");
            doPost(request, response);
        } catch (Exception e) {

        }
    }

    @Override
    public NodeTransfer getNodeByPath(String path, Integer depth) {
        Node node = nodeListService.getNodeByPath(path, depth);
        NodeTransfer result = transformNode(node);

        return result;
    }

    private NodeTransfer transformNode(Node node){
        NodeTransfer result = new NodeTransfer();
        result.setRoot(node.isRoot());
        result.setName(node.getName());
//        if (node.getParent()!= null){
//            result.setParent(transformNode(node.getParent()));
//        }
        if (!node.getChilds().isEmpty()){
            ArrayList<NodeTransfer> childNodeTransfers = new ArrayList<NodeTransfer>(node.getChilds().size());
            for (Node child: node.getChilds()){
                NodeTransfer childTransfer = transformNode(child);
                childTransfer.setParent(result);
                childNodeTransfers.add(childTransfer);

            }
            result.setChilds(childNodeTransfers);
        }
        result.setProperties(node.getProperties());
        return result;
    }
}
